package xin.marcher.module.seller.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.seller.domain.dto.SellerGroupRelationDTO;
import xin.marcher.module.seller.domain.dto.SellerGroupRelationResultDTO;
import xin.marcher.module.seller.domain.request.SellerGroupRelationRequest;
import xin.marcher.module.seller.service.SellerGroupRelationService;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/seller/relation")
@Slf4j
public class SellerGroupRelationController {

    @Resource
    private SellerGroupRelationService sellerGroupRelationService;

    @PostMapping("/save")
    public JsonResult<SellerGroupRelationResultDTO> saveSellerGroupRelationInfo(@RequestBody SellerGroupRelationRequest request) {
        try {
            SellerGroupRelationResultDTO relationResultDTO = sellerGroupRelationService.saveSellerGroupRelationInfo(request);
            return JsonResult.buildSuccess(relationResultDTO);
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @GetMapping("/queryRelations")
    public JsonResult<List<SellerGroupRelationDTO>> queryAccountsBySellerId(@RequestBody SellerGroupRelationRequest request) {
        try {
            List<SellerGroupRelationDTO> accountDTOS = sellerGroupRelationService.querySellerGroupRelationInfo(request);
            return JsonResult.buildSuccess(accountDTOS);
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public JsonResult<Boolean> deleteAccount(SellerGroupRelationRequest request) {
        try {
            return JsonResult.buildSuccess(sellerGroupRelationService.deleteBySellerId(request));
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
